package com.nisiken.aws.copy;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import com.amazonaws.services.dynamodb.model.ScanRequest;
import com.amazonaws.services.dynamodb.model.ScanResult;
import com.amazonaws.services.dynamodb.model.TableDescription;

public class CopyDynamoDBTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		CopyDynamoDB.init();
	}

	public void バックアップ() {
		for (String tableName : CopyDynamoDB.dynamoDBFrom.listTables().getTableNames()) {
			if ("healtyGoal2".equals(tableName)) { //healtyGoal2 has un support serializable data type "byte"
				CopyDynamoDB.saveTableDescription(tableName, tableName + "Desc");
				continue;
			}
			CopyDynamoDB.saveTableDescription(tableName, tableName + "Desc");
			CopyDynamoDB.saveTableItems(tableName, tableName + "Items");
		}
	}
	
	public void リストア() {
		String[] tableNames = {"healtyGoal", "healtyUser", "healtyWeight"};
		for (String tableName : tableNames) {
			CopyDynamoDB.readTableDescription(tableName + "Desc");
			CopyDynamoDB.readTableItems(tableName, tableName + "Items");
		}
	}
	
	public void テーブルをコピーする() {
		ScanRequest request = new ScanRequest("healtyGoal");
    	ScanResult result = CopyDynamoDB.dynamoDBFrom.scan(request);
		Assert.assertEquals(18, result.getCount().intValue());
    	
		CopyDynamoDB.copyTable("healtyGoal");
		
		result = CopyDynamoDB.dynamoDBTo.scan(request);
		Assert.assertEquals(18, result.getCount().intValue());
	}
	
	@Test
	public void テーブル情報をファイルに保存する() {
		CopyDynamoDB.saveTableDescription("healtyGoal", "healtyGoalDisc");
		TableDescription desc = (TableDescription) CopyDynamoDB.readObject("healtyGoalDisc");
		Assert.assertEquals("healtyGoal", desc.getTableName());
		Assert.assertEquals("name", desc.getKeySchema().getHashKeyElement().getAttributeName());
	}
	
	public void テーブルを生成する() {
		CopyDynamoDB.readTableDescription("healtyGoalDisc");
	}
	
	public void テーブルアイテムをファイルに保存する() {
		CopyDynamoDB.saveTableItems("healtyGoal", "healtyGoalItems");
		ScanResult items = (ScanResult) CopyDynamoDB.readObject("healtyGoalItems");
		Assert.assertTrue(items.getCount() > 2);
	}
	
	public void テーブルアイテムを生成する() {
		CopyDynamoDB.readTableItems("healtyGoal", "healtyGoalItems");
	}
	
}
